unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  but = array[0..19] of TButton;
var
  Form1: TForm1;
  b : array[0..19] of but;
  num : integer;


implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i, j : integer;
begin
  for i:=0 to 19 do
    for j:=0 to 19 do
    begin
      b[i][j] := TButton.Create(Form1);
      b[i][j].Height := 20;
      b[i][j].Width := 20;
      b[i][j].Top := (b[i][j].Height)*(i mod 20);
      b[i][j].Left := (b[i][j].Width)*(j mod 20);
      b[i][j].Parent := Form1;
      b[i][j].OnClick := Button1Click;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i, j : integer;
k, l : integer;
v, g, c : integer;
  procedure WinX(v, g : integer);
  var
    c, t, t1 : integer;
  begin
    c := 0;
    t := v;
    while (b[v][g].Caption = 'X') do
    begin
      v := v-1;
      c := c+1;
    end;
    v := t+1;
    while (b[v][g].Caption = 'X') do
    begin
      v := v+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    v := t;
    t := g;
    while (b[v][g].Caption = 'X') do
    begin
      g := g-1;
      c := c+1;
    end;
    g := t+1;
    while (b[v][g].Caption = 'X') do
    begin
      g := g+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    g := t;
    t := g;
    t1 := v;
    while (b[v][g].Caption = 'X') do
    begin
      g := g-1;
      v := v-1;
      c := c+1;
    end;
    g := t+1;
    v := t1+1;
    while (b[v][g].Caption = 'X') do
    begin
      g := g+1;
      v := v+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    t := g;
    t1 := v;
    while (b[v][g].Caption = 'X') do
    begin
      g := g+1;
      v := v-1;
      c := c+1;
    end;
    g := t;
    v := t1;
    while (b[v][g].Caption = 'X') do
    begin
      g := g-1;
      v := v+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
  end;
    procedure WinO(v, g : integer);
  var
    c : integer;
  begin
    c := 0;
    while (b[v][g].Caption = 'O') do
    begin
      v := v-1;
      c := c+1;
    end;
    while (b[v][g].Caption = 'O') do
    begin
      v := v+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    while (b[v][g].Caption = 'O') do
    begin
      g := g+1;
      c := c+1;
    end;
    while (b[v][g].Caption = 'O') do
    begin
      g := g-1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    while (b[v][g].Caption = 'O') do
    begin
      g := g+1;
      v := v+1;
      c := c+1;
    end;
    while (b[v][g].Caption = 'O') do
    begin
      g := g-1;
      v := v-1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
    while (b[v][g].Caption = 'O') do
    begin
      g := g+1;
      v := v-1;
      c := c+1;
    end;
    while (b[v][g].Caption = 'O') do
    begin
      g := g-1;
      v := v+1;
      c := c+1;
    end;
    if (c >= 5) then
      Application.MessageBox('Win', 'Game');
  end;
begin
  for i:=0 to 19 do
    for j:=0 to 19 do
    begin
      if (Sender=b[i][j]) then
      begin
        b[i][j].Enabled := False;
        v := i;
        g := j;
        c := 0;
        if (num mod 2 = 0) then
        begin
          b[i][j].Caption := 'X';
          num := num+1;
          WinX(v, g);
        end
        else begin
          b[i][j].Caption := 'O';
          WinO(v, g);
          num := num+1;
        end;
      end;
    end;
end;

end.
